#!/bin/sh

prefix="/opt"
PATH=${prefix}/bin:${prefix}/sbin:/sbin:/bin:/usr/sbin:/usr/bin

if [ -z "`pidof portmap`" ] ; then
	echo 'Error: portmap not started'
	exit 1
fi

start() {
	echo 'Starting NFS server'
	modprobe nfsd
	#for module in sunrpc lockd exportfs nfsd; do
	#       insmod /lib/modules/3.4.113/${module}.ko
	#done;
	mkdir -p /opt/var/lib/nfs/sm
	mkdir -p /opt/var/lib/nfs/sm.bak
	mkdir -p /opt/var/lib/nfs/rpc_pipefs
	echo -n > /opt/var/lib/nfs/etab
	echo -n > /opt/var/lib/nfs/rmtab
	echo -n > /opt/var/lib/nfs/xtab
	mount -t rpc_pipefs rpc_pipefs /opt/var/lib/nfs/rpc_pipefs
	mount -t nfsd nfsd /proc/fs/nfsd
	echo 65536 > /proc/fs/nfsd/max_block_size
	exportfs -ra
	rpc.nfsd --no-nfs-version 4 --no-tcp 1
	rpc.mountd --no-nfs-version 4 --no-tcp
	}

stop() {
	echo 'Stopping NFS server'
	killall -q rpc.mountd
	killall -q -2 nfsd
	exportfs -ua
	umount /proc/fs/nfsd 2>/dev/null
	umount /opt/var/lib/nfs/rpc_pipefs 2>/dev/null
	rm -f /opt/var/lib/nfs/etab
	}

status() {
	if [ -n "`pidof nfsd`" ] && [ -n "`pidof rpc.mountd`" ] ; then
		echo 'NFS server is running'
	else
		echo 'NFS server is stopped'
	fi
	}

reload() {
	echo 'Reloading /opt/etc/exports'
	exportfs -r
	}

case "$1" in
	start)
		start
		;;
	stop)
		stop
		;;
	restart)
		stop
		sleep 2
		start
		;;
	status)
		status
		;;
	reload)
		reload
		;;
	*)
		echo "Usage: $0 (start|stop|restart|reload|status)"
		exit 1
		;;
esac

exit 0
